from bs4 import BeautifulSoup
import requests
import csv


url = "http://www.xinfadi.com.cn/priceDetail.html"
# User-Agent
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/100.0.4896.127 Safari/537.36 "
}
resp = requests.get(url, headers=headers)
page_context = resp.text

# 文件
file = open("../files/price.csv", mode="w")
csvwriter = csv.writer(file)

# bs4解析数据
bs_page = BeautifulSoup(page_context, "html.parser")
print(bs_page)
body = bs_page.find("tbody", attrs={"id": "tableBody", "class": "ul"})
print(body)
trs = body.find_all("tr")
print(trs)
for i in trs:
    print(i)

# 关闭链接
resp.close()